package mental_connect.backend.backend_ym.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import mental_connect.backend.backend_ym.entity.AccountInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean("accountInfo")
    public RedisTemplate<String, AccountInfo> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, AccountInfo> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置Key的序列化
        template.setKeySerializer(new StringRedisSerializer());

        // 设置Value的序列化与反序列化
        ObjectMapper objectMapper = new ObjectMapper();
        Jackson2JsonRedisSerializer<AccountInfo> serializer = new Jackson2JsonRedisSerializer<>(AccountInfo.class);
        serializer.setObjectMapper(objectMapper);
        template.setValueSerializer(serializer);

        return template;
    }
}